// pages/category/category.js
var common = require("../../utils/Conmon.js");

var shopID = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CategoryStatus: 0,
    CategoryList: {},
    SubCategoryList: {},
    TitleClass: '',
    TitleURL: '',
    TitleName: '',
    shopID: 0,
    allData: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    common.pageInit(function() {});
    if (options.shopID > 0) {
      shopID = options.shopID;
      this.setData({
        shopID: shopID
      })
    }
    GetProductCategory(this, 0);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  //绑定第二第三层
  bindSon: function(parentID) {
    var allData = this.data.allData;
    var secondList = new Array();
    if (allData != null && allData.length > 0) {
      for (var i = 0; i < allData.length; i++) {
        if (parentID == allData[i].ParentID) {
          var thirdList = new Array();
          for (var j = 0; j < allData.length; j++) {
            if (allData[j].ParentID == allData[i].CatalogID) {
              thirdList.push(allData[j]);
            }
          }
          allData[i].Catalogs = thirdList;
          secondList.push(allData[i]);
        }
      }
    }
    this.setData({
      SubCategoryList: secondList
    })

  },
  /**
   * 获取商品二三级分类
   */
  onGetProductCategory: function(event) {
    var catalogId = event.currentTarget.id;
    var catalogName = event.currentTarget.dataset.name;
    this.setData({
      TitleClass: 'golist-btn',
      TitleURL: '/pages/goodsList/goodsList?CatalogId=' + catalogId + "&shopID=" + shopID,
      TitleName: '查看' + catalogName + '类商品列表'
    })
    this.bindSon(catalogId);

  },

  /**
   * 跳转到商品列表
   */
  onToGoodsList: function(event) {
    wx.redirectTo({
      url: '/pages/goodsList/goodsList?CatalogId=' + event.currentTarget.id + "&shopID=" + shopID
    })
  }
})

//获取商品分类
function GetProductCategory(obj, catalogId) {
  wx.showLoading({
    title: '加载中...',
    mask: true
  });

  var that = obj;
  var globalData = getApp().globalData;
  var custId = globalData.custID;
  var url = globalData.apiurl + '/mobile/api/Product/GetAllProductCategory';

  wx.request({
    url: url,
    data: {
      "custID": custId,
      "status": that.data.CategoryStatus,
      "shopID": shopID
    },
    method: 'GET',
    header: {
      'content-type': 'application/json',
      'X_ManuName': globalData.manuName
    },
    success: function(res) {
      var list = res.data.content;
      //绑定第一层
      var parentList = new Array();
      if (list != null && list.length > 0) {
        for (var i = 0; i < list.length; i++) {
          if (list[i].ParentID == 0) {
            parentList.push(list[i]);
          }
        }

      }
      that.setData({
        allData: list,
        CategoryList: parentList
      });
      if (parentList.length > 0) {
        that.bindSon(parentList[0].CatalogID);
      }
      wx.hideLoading();

    },
    fail: function() {
      wx.hideLoading();
      wx.showToast({
        title: '获取商品分类失败',
        duration: 2000
      })
    },
    complete: function() {
      //complete
    }
  })
}

//获取商品列表
function GetProductList(id) {
  return new Promise(function(resolve, reject) {
    var globalData = getApp().globalData;
    var custId = globalData.custID;
    var url = globalData.apiurl + '/mobile/api/Product/GetProductList';

    wx.request({
      url: url,
      data: {
        ProductCatalog: id,
        CustID: custId,
        Status: 1
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'X_ManuName': globalData.manuName
      },
      success: function(res) {
        resolve(res.data.content);
      },
      fail: function() {
        wx.showToast({
          title: '获取商品列表失败',
          duration: 2000
        })
      },
      complete: function() {
        //complete
      }
    })
  });
}